package com.example.campus.api_controller;


import com.example.campus.dao.table_gateway.ActivityMapper;
import com.example.campus.dao.model.Activity;
import com.example.campus.page_controller.dto.ResponseResult;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Set;

import static com.example.campus.common.BusinessConstants.API_BASE;

@RestController
@RequestMapping(API_BASE + "/activities")
public class ActivityController {

    @Autowired
    ActivityMapper activityMapper;

    @PostMapping
    public ResponseResult<String> addActivity(@RequestBody Activity activity) {
        activity.setCreatedTime(new Date());
        activityMapper.addActivity(activity);

        return ResponseResult.ok();
    }

//    @GetMapping
//    public ResponseResult<Set<Activity>> getActivities() {
//        var activities =  activityMapper.allActivities();
//        return ResponseResult.ok(activities);
//    }

    @GetMapping
    public ResponseResult<List<Activity>> getActivitiesByNameLike(@RequestParam("name") String name) {
        var activities =  activityMapper.getAllActivitiesByNameLike("%"+name+"%");

        return ResponseResult.ok(activities);
    }

    @GetMapping("{id}")
    public  ResponseResult<Activity> getActivityById(@PathVariable("id")long id){
        var activity = activityMapper.getActivityById(id);
        return ResponseResult.ok(activity);
    }

    @PutMapping
    public ResponseResult<String> updateActivity(@Valid @RequestBody Activity activity) {
        activityMapper.updateActivity(activity);

        return ResponseResult.ok();
    }

    @DeleteMapping("{id}")
    public ResponseResult<String> deleteActivityById(@PathVariable("id")long id){
       activityMapper.deleteActivityById(id);
        return ResponseResult.ok();
    }

    @PostMapping("{id}/publish")
    public  ResponseResult<String> publishActivityById(@PathVariable("id")long id){
        var activity = new Activity();
        activity.setId(id);
        activity.setStatus(1);
        updateActivity(activity);

        return ResponseResult.ok();
    }
}
